﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class showCategories : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string startingWord = Request.QueryString["c"];

        string[] categoryTree = startingWord.Split('/');
        int length = categoryTree.Length;

        if (categoryTree.Length >= 2)
        {
            SqlDataSource1.SelectCommand = string.Format(@"
SELECT * FROM [Category] 
WHERE ([Name] LIKE '{0}%') AND (parent = '{1}')", categoryTree[length - 1], categoryTree[length - 2]);
        }
        else
        {
            SqlDataSource1.SelectCommand = string.Format(@"
SELECT * FROM [Category] 
WHERE ([Name] LIKE '{0}%') AND (parent IS NULL)", startingWord);
        }

    }

    public void categoryList_DataBound(object sender, EventArgs e)
    {
        var i = categoryList.Items.Count;

        if (categoryList.Items.Count == 0)
            categoryList.Visible = false;
        else if (categoryList.Items.Count < 5)
            categoryList.Rows = 4;
        else
            categoryList.Rows = Math.Min(categoryList.Items.Count, 10);
    }
}